/* 
    Purpose: Using the 1960-1990 Censuses,
             this file creates mother income scores at 
             the occ-only,race-only, south-only, 
             occ x south, occ x race, race x south levels. 
             Income scores are then merged (along with
             1940 income scores) by level of variation.

    Creates: All output files have the naming structure
            "incomescores_mothers_1940_to1990_" 
*/

clear
set more off
cd "$Mydirectory1/1_DataSources/CensusData/"

**---------------------------------------------------------------------------**
**---------------------------------------------------------------------------**

****************************
* SAVE 1940 INCOME SCORES
****************************

/* Note: All 1940 files are brought in 
         from the NBER server. */

* Variations without occupation 
    foreach x in byrace bysouth byr_bys {
    
    use ./output/avgincomes_mothers1940_`x', clear 
    
    assert HHinc!=.
    rename HHinc motherinc_1940_`x'
    
    tempfile file_1940_`x'
    save `file_1940_`x''
    
    }

* Variations with occupation 
    /* Note: Must use income scores that
             incorporate Collins and Wanamaker
             (2017) adjustments to farmer and 
             self-employed income.*/
    foreach x in byocc byocc_byr byocc_bys {

    use ./output/avgincomes_mothers1940_`x'_CWfix, clear 
    
    assert mom_HHinc_CWfix_`x'!=.
    rename mom_HHinc_CWfix_`x' motherinc_1940_`x'
    label var motherinc_1940_`x' "Mother HH income, 1940, CW fix"
    
    drop HHinc* imputed*
    
    tempfile file_1940_`x'
    save `file_1940_`x''    
    
    }
    
**---------------------------------------------------------------------------**
**---------------------------------------------------------------------------**

*************************
* TEMPLATES (1960-1990)
*************************

/* Note: A template is necessary for variations with
         occupation. Census microdata does not always 
         have all 28 coarsened occupations, so will use 
         the template below that does have all occupations. 
*/  
    use ./code/OtherCensus_RawData/motheroccej_template_occs.dta, clear 

* Template 1: occupation
preserve 
    
    tempfile template_byocc
    save `template_byocc'
        
restore 

* Template 2: occupation x race 
preserve 
    
    expand 2, gen(race)
    replace race=race+1
    
    tempfile template_byocc_byr
    save `template_byocc_byr'
        
restore 

* Template 3: occupation x south 
preserve 
    
    expand 2, gen(south_merge)
    
    tempfile template_byocc_bys
    save `template_byocc_bys'
        
restore

**------------------------------**
**------------------------------**

*****************************************
* COLLAPSE (VARIATIONS W/0 OCCUPATION)
*****************************************

foreach y in 6 7 8 9 {

    use ./output/Census19`y'0_mothers_ages30to50.dta, clear   
    
    foreach x in byrace bysouth byr_bys {
    
    if "`x'"=="byrace" local varlist "race"
    if "`x'"=="bysouth" local varlist "south_merge"
    if "`x'"=="byr_bys" local varlist "race south_merge"
    
    preserve 
    collapse (mean) HHinc [aw=perwt], by(`varlist') 
    
    assert HHinc!=.
    rename HHinc motherinc_19`y'0_`x'
    label var motherinc_19`y'0_`x' "Mother HH income, 19`y'0, `varlist'"
    
    tempfile file_19`y'0_`x'
    save `file_19`y'0_`x''
    
    restore 
    
    }
    }
    
*****************************************
* COLLAPSE (VARIATIONS WITH OCCUPATION)
*****************************************
    
foreach y in 6 7 8 9 {
    
    use ./output/Census19`y'0_mothers_ages30to50.dta, clear   
    
    foreach x in byocc byocc_byr byocc_bys  {
    
    if "`x'"=="byocc" local varlist "motheroccej"
    if "`x'"=="byocc_byr" local varlist "motheroccej race"
    if "`x'"=="byocc_bys" local varlist "motheroccej south_merge"   
    
    preserve 
    collapse (mean) HHinc [aw=perwt], by(`varlist') 
    
    assert HHinc!=.
    
    merge 1:1 `varlist' using `template_`x'', assert(2 3) nogen //Note: merge won't be perfect.
    
    rename HHinc motherinc_19`y'0_`x'
    label var motherinc_19`y'0_`x' "Mother HH income, 19`y'0, `varlist'"
    
    tempfile file_19`y'0_`x'
    save `file_19`y'0_`x''
        
    
    restore 
    
    }
    }
        
****************************************
* MERGE/SAVE (BY LEVEL OF VARIATION)
****************************************

    foreach x in byrace bysouth byr_bys byocc byocc_byr byocc_bys  {
    
        if "`x'"=="byocc" local varlist "motheroccej"
        if "`x'"=="byocc_byr" local varlist "motheroccej race"
        if "`x'"=="byocc_bys" local varlist "motheroccej south_merge"
        if "`x'"=="byrace" local varlist "race"
        if "`x'"=="bysouth" local varlist "south_merge"
        if "`x'"=="byr_bys" local varlist "race south_merge"    

        use `file_1960_`x'', clear
        foreach y in 4 7 8 9 {
            merge 1:1 `varlist' using `file_19`y'0_`x'', assert(3) nogen
        }
    
    save ./output/incomescores_mothers_1940_to1990_`x'.dta, replace 
    
    }
